﻿using System.Windows.Input;
using MvvmFx.Common.ViewModels;
using MvvmFx.Common.ViewModels.Behaviors.LoadingSaving;
using MvvmFx.Samples.HowTos.ChangeTracking.ViewModels;
using MvvmFx.Samples.HowTos.PropertyHandlers.ViewModels;
using MvvmFx.Samples.HowTos.PropertyRehandlers;
using MvvmFx.Samples.HowTos.Validation;
using Microsoft.Practices.Unity;

namespace MvvmFx.Samples.HowTos
{
    /// <summary>
    /// Singleton class to control the module and expose 
    /// it's state values, settings, and commands
    /// </summary>
    public class ApplicationController : ViewModelBase
    {
        #region Instance Variables

        private readonly IUnityContainer _container;
        private ViewModelCollection _howTos;
        private ViewModelBase _selectedHowTo;

        #endregion

        #region Singleton Constructor

        private static readonly ApplicationController _instance
            = new ApplicationController();

        static ApplicationController()
        {

        }

        /// <summary>
        /// Singleton Constructor 
        /// </summary>
        protected ApplicationController()
        {
            // Create the UnityContainer
            _container = new UnityContainer();

            RegisterTypes();

            BuildHowTos();
        }

        #endregion

        #region Public Properties

        /// <summary>
        /// Gets the value of the <see cref=”_instance”/> property.
        /// </summary>
        public static ApplicationController Instance
        {
            get { return _instance; }
        }

        /// <summary>
        ///  Gets the value of the <see cref=”Container”/> property.
        /// </summary>
        public IUnityContainer Container
        {
            get { return _container; }
        }

        /// <summary>
        /// Gets the value of the <see cref=”HowTos”/> property.
        /// </summary>
        public ViewModelCollection HowTos
        {
            get { return _howTos; }
        }

        /// <summary>
        /// Gets or sets the value of the <see cref=”SelectedHowTo”/> property.
        /// </summary>
        public ViewModelBase SelectedHowTo
        {
            get { return _selectedHowTo; }
            set
            {
                if (_selectedHowTo != value)
                {
                    _selectedHowTo = value;
                    OnPropertyChanged("SelectedHowTo");
                }
            }
        }

        #endregion

        #region Public Commands


        #endregion

        #region Private Methods

        private void BuildHowTos()
        {
            _howTos = new ViewModelCollection();

            var propertyHandlers = new PropertyHandlersViewModel { Key = "Handle Property Changes" };
            _howTos.Add(propertyHandlers);

            var contactDetails = _container.Resolve<ContactDetailsViewModel>();
            contactDetails.Key = "Track changes in child objects";
            contactDetails.Load();
            _howTos.Add(contactDetails);

            var boolVM = new BoolViewModel { Key = "Bind to Commands" };
            _howTos.Add(boolVM);

            var deliveryVM = new DeliveryViewModel { Key = "Use Validation Rules" };
            _howTos.Add(deliveryVM);
        }

        private void RegisterTypes()
        {
            _container.RegisterType(typeof(IViewModelLoader<ContactDetailsViewModel>), typeof(ContactDetailsLoader));
            _container.RegisterType(typeof(IViewModelSaver<ContactDetailsViewModel>), typeof(ContactDetailsSaver));
        }

        #endregion
    }
}